const express = require('express')
const router = express.Router();
const db = require('../../utils/db')

//查询所有菜品库存
router.get('/getInventory', function (req, res, next) {
    console.log(1);
    let mysql = `SELECT  check_num,menu_name,menu_size,menu_amount,check_time,menu_price 
    FROM stock_check  JOIN menu_file 
    ON stock_check.check_type=menu_file.menu_id;`
    db(mysql, null, function (err, data) {
        console.log(err);
        if (err) {
            return res.json({
                code: 500,
                msg: '查询库存失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '查询库存成功',
            data: data
        })

    })

})

//查询库存过低  或 库存过高
router.post('/getSearch', function (req, res, next) {
    let mysql = ''
    if (req.body.menu_amount == 50) {
        mysql = `SELECT  check_num,menu_name,menu_size,menu_amount,check_time 
             FROM stock_check  JOIN menu_file 
             ON stock_check.check_type=menu_file.menu_id 
             WHERE menu_amount >=?`
    } else if (req.body.menu_amount == 10) {
        mysql = `SELECT  check_num,menu_name,menu_size,menu_amount,check_time 
            FROM stock_check  JOIN menu_file 
            ON stock_check.check_type=menu_file.menu_id 
            WHERE menu_amount <=?`

    }
    db(mysql, req.body.menu_amount, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '查询数据失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '查询数据成功',
            data: data
        })

    })



})

// 修改菜品库存
router.post('/updateInventory', function (req, res, next) {
    let mysql = `UPDATE menu_file SET menu_amount=? WHERE menu_name=? `
    db(mysql, [req.body.menu_amount, req.body.menu_name], function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '数据库操作失败,请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '修改库存成功',
            data: data
        })

    })


})




module.exports = router